﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using Data.Car_Rental;
using Web.Commons.Helpers;
namespace Background.Helper
{
    /// <summary>
    /// 前台HtmlHelper扩展
    /// </summary>
    public static class ViewHelper
    {
        public static MvcHtmlString HelpCategoryForSelectList(this HtmlHelper html, int? categoryID)
        {
            StringBuilder sb = new StringBuilder();
            List<Entity.Car_Rental.HelperCenterCategory> helperCenterCategoryList = Data.Car_Rental.HelperCenterCategoryRepository.Instance;
            sb.Append("<select name='HelpCategoryID' id='HelpCategoryID'>");
            foreach (var i in helperCenterCategoryList)
                if (categoryID.HasValue && i.HelpCategoryID == categoryID)
                    sb.AppendFormat("<option value='{0}' selected='selected'>{1}</option>", i.HelpCategoryID, i.Name);
                else
                    sb.AppendFormat("<option value='{0}'>{1}</option>", i.HelpCategoryID, i.Name);
            sb.Append("</select>");
            return MvcHtmlString.Create(sb.ToString());
        }

        /// <summary>
        /// 分类选择器
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static MvcHtmlString CategoryForSelectList(this HtmlHelper html)
        {
            return CategoryForSelectList(html, null);
        }
        /// <summary>
        /// 分类选择器
        /// </summary>
        /// <param name="html"></param>
        /// <param name="categoryID"></param>
        /// <returns></returns>
        public static MvcHtmlString CategoryForSelectList(this HtmlHelper html, int? categoryID)
        {
            StringBuilder sb = new StringBuilder();
            List<Entity.Car_Rental.Category> categoryList = Data.Car_Rental.CategoryRepository.Instance;
            sb.Append("<select name='CategoryID' id='CategoryID'>");
            foreach (var i in categoryList)
                if (categoryID.HasValue && i.ID == categoryID)
                    sb.AppendFormat("<option value='{0}' selected='selected'>{1}</option>", i.ID, i.Name);
                else
                    sb.AppendFormat("<option value='{0}'>{1}</option>", i.ID, i.Name);
            sb.Append("</select>");
            return MvcHtmlString.Create(sb.ToString());
        }


        public static MvcHtmlString HelpCategoryForSelectList(this HtmlHelper html)
        {
            return HelpCategoryForSelectList(html, null);
        }


        /// <summary>
        /// 属性类型与属性选择器模块
        /// </summary>
        /// <param name="html"></param>
        /// <param name="productPropertyTypeID"></param>
        /// <returns></returns>
        public static MvcHtmlString PropertyType_PropertyForPanel(this HtmlHelper html, int? productPropertyTypeID)
        {
            IProductPropertyRepository iProductPropertyRepository = new ProductPropertyRepository();
            List<Entity.Car_Rental.ProductProperty> productPropertyList = ProductPropertyRepository.Instance.Where(i => i.PropertyTypeID == 1).ToList();

            StringBuilder sb = new StringBuilder();
            sb.Append(PropertyTypeSelectListToString(productPropertyTypeID));

            // 将ascx页面视图返回
            sb.Append("<span id='productProperty'>" + HtmlExtensions.RenderPartialToString(html.ViewContext, "/Views/Product/GetPropertyList.ascx", productPropertyList) + "</span>");

            //添加JS事件，实现异步加载
            sb.Append(@" <script>
                            $(function () {
                                $('#ProductPropertyTypeID').change(function () {
                                    $('#productProperty').empty();              
                                    $.ajax({
                                        type: 'GET',
                                        data: { typeID: $(this).val() },
                                        url: '/product/GetPropertyList',
                                        success: function (data) {
                                            $('#productProperty').html(data);
                                        }
                                    })
                                });
                            });
                        </script>");
            return MvcHtmlString.Create(sb.ToString());
        }
        public static MvcHtmlString PropertyType_PropertyForPanel(this HtmlHelper html)
        {
            return PropertyType_PropertyForPanel(html, null);
        }
        /// <summary>
        /// 得到属性分类下拉列表框
        /// </summary>
        /// <param name="productPropertyTypeID"></param>
        /// <returns></returns>
        private static string PropertyTypeSelectListToString(int? productPropertyTypeID)
        {
            IProductPropertyTypeRepository iProductPropertyTypeRepository = new ProductPropertyTypeRepository();
            StringBuilder sb = new StringBuilder();
            List<Entity.Car_Rental.ProductPropertyType> productPropertyTypeList = ProductPropertyTypeRepository.Instance.ToList();
            sb.Append("<select name='ProductPropertyTypeID' id='ProductPropertyTypeID'>");
            foreach (var i in productPropertyTypeList)
                if (productPropertyTypeID.HasValue && i.ID == productPropertyTypeID)
                    sb.AppendFormat("<option value='{0}' selected='selected'>{1}</option>", i.ID, i.Name);
                else
                    sb.AppendFormat("<option value='{0}'>{1}</option>", i.ID, i.Name);
            sb.Append("</select>");
            return sb.ToString();
        }
        /// <summary>
        /// 得到属性分类下拉列表框
        /// </summary>
        /// <param name="html"></param>
        /// <param name="productPropertyTypeID"></param>
        /// <returns></returns>
        public static MvcHtmlString PropertyTypeForSelectList(this HtmlHelper html, int? productPropertyTypeID)
        {
            return MvcHtmlString.Create(PropertyTypeSelectListToString(productPropertyTypeID));
        }
        /// <summary>
        /// 得到属性分类下拉列表框
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static MvcHtmlString PropertyTypeForSelectList(this HtmlHelper html)
        {
            return PropertyTypeForSelectList(html, null);
        }
    }
}